package com.cloudwise.gproject.service.impl;

import com.cloudwise.gproject.entity.Business;
import com.cloudwise.gproject.dao.BusinessDao;
import com.cloudwise.gproject.service.BusinessService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (Business)表服务实现类
 *
 * @author makejava
 * @since 2021-03-22 09:10:12
 */
@Service("businessService")
public class BusinessServiceImpl implements BusinessService {
    @Resource
    private BusinessDao businessDao;

    /**
     * 通过ID查询单条数据
     *
     * @param businessId 主键
     * @return 实例对象
     */
    @Override
    public Business queryById(Integer businessId) {
        return this.businessDao.queryById(businessId);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<Business> queryAllByLimit(int offset, int limit) {
        return this.businessDao.queryAllByLimit(offset, limit);
    }

    @Override
    public PageInfo queryAll(Integer page, Integer limit, Business business) {
        //设值查询查询
        PageHelper.startPage(page,limit);
        PageInfo PageInfo = new PageInfo(businessDao.queryAll(business));
        return PageInfo;
    }

    @Override
    public List<Map<String, Object>> findAllDictionary() {
        return businessDao.findAllDictionary();
    }

    /**
     * 新增数据
     *
     * @param business 实例对象
     * @return 实例对象
     */
    @Override
    public Business insert(Business business) {
        this.businessDao.insert(business);
        return business;
    }

    /**
     * 修改数据
     *
     * @param business 实例对象
     * @return 实例对象
     */
    @Override
    public Business update(Business business) {
        this.businessDao.update(business);
        return this.queryById(business.getBusinessId());
    }

    /**
     * 通过主键删除数据
     *
     * @param businessId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer businessId) {
        return this.businessDao.deleteById(businessId) > 0;
    }

    /**
     * @create by: 卫子健
     * @description: 通过多个id删除多个数据?
     * @create time: 2021/3/19 23:13
     */
    @Override
    public int deleteIds(String deleteIds) {
        return 0;
    }
    /** 
     * @create by: 卫子健
     * @description: 得到首页数据 
     * @create time: 2021/3/22 9:21
     * @param  
     * @return  
     */
    @Override
    public List<Integer> getIndexData() {
        //获取当前时间字符串
        LocalDateTime now = LocalDateTime.now();
        String nowStr = now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        //分割获取当天
        String[] s = nowStr.split(" ");
        String today = s[0];
        //不能为空
        Integer businessCount = (businessDao.getBusinessCount(today) != null) ? businessDao.getBusinessCount(today) : 0;
        Integer visitCount = (businessDao.getVisitCount(today) != null) ? businessDao.getVisitCount(today) : 0;
        Integer money = (businessDao.getMoney(today) != null) ? businessDao.getMoney(today) : 0;
        //创建一个list
        ArrayList<Integer> list = new ArrayList<>();
        list.add(businessCount);
        list.add(visitCount);
        list.add(money);
        return list;
    }
    /** 
     * @create by: 卫子健
     * @description: 业务量走势图 
     * @create time: 2021/3/22 9:47
     * @param
     * @return  
     */
    @Override
    public Map<String, Object> showBusinessChart() {
        List<Map<String, Object>> maps = businessDao.showBusinessChart();
        //定义map装载所查出的信息
        HashMap<String, Object> map = new HashMap<>();
        //定义x轴
        String[] monthX = new String[maps.size()];
        //定义y轴
        Integer[] businessCountY = new Integer[maps.size()];
        for (int i = 0; i < maps.size(); i++) {
            monthX[i] = maps.get(i).get("month").toString();
            businessCountY[i] = Integer.parseInt(maps.get(i).get("businessCount").toString());
        }
        map.put("monthX", monthX);
        map.put("businessCountY", businessCountY);
        return map;
    }

    @Override
    public Map<String, Object> showPerformanceRanking() {
        List<Map<String, Object>> maps = businessDao.showPerformanceRanking();
        //定义map装载所查出的信息
        HashMap<String, Object> map = new HashMap<>();
        //定义x轴
        String[] loginNameX = new String[maps.size()];
        //定义y轴
        Double[] performanceY = new Double[maps.size()];
        for (int i = 0; i < maps.size(); i++) {
            loginNameX[i] = maps.get(i).get("login_name").toString();
            performanceY[i] = Double.parseDouble(maps.get(i).get("performance").toString());
        }
        map.put("loginNameX", loginNameX);
        map.put("performanceY", performanceY);
        return map;
    }

    /** 
     * @create by: 卫子健
     * @description: 各种业务类型的数量 
     * @create time: 2021/3/25 13:05
     * @param  
     * @return  
     */
    @Override
    public List<Long> businessEchart() {
        List<Map<String, Object>> list = businessDao.businessEchart();
        List<Long> list1 = new ArrayList<>();
        long patent = 0, trademark = 0, copyright = 0, property = 0;
        for (Map<String, Object> map : list) {
            String a = map.get("business_top_type").toString();
            switch (a) {
                case "专利":
                    patent = (Long) map.get("count(*)");
                    break;
                case "商标":
                    trademark = (Long) map.get("count(*)");
                    break;
                case "版权":
                    copyright = (Long) map.get("count(*)");
                    break;
                case "知识产权":
                    property = (Long) map.get("count(*)");
                    break;
            }
        }
        list1.add(patent);
        list1.add(trademark);
        list1.add(copyright);
        list1.add(property);
        return list1;
    }

    /** 
     * @create by: 卫子健
     * @description: 按照代理人展示业务数量 
     * @create time: 2021/3/25 13:20
     * @param loginName 
     * @return com.cloudwise.trademark.util.Result 
     */
    @Override
    public Map<String, Object> showBusinessChartByAgent(String loginName) {
        List<Map<String, Object>> list = businessDao.showBusinessChartByAgent(loginName);
        Map<String, Object> map = new HashMap<>();
        //X轴
        String[] X = new String[list.size()];
        //y轴
        Long[] Y = new Long[list.size()];
        for (int i = 0; i < list.size(); i++) {
            X[i] = list.get(i).get("month").toString();
            Y[i] = (Long) list.get(i).get("count");
        }
        map.put("X", X);
        map.put("Y", Y);
        return map;
    }

    /** 
     * @create by: 卫子健
     * @description: 按照日期区间显示业务数量 
     * @create time: 2021/3/25 13:22
     * @param startTime
     * @param endTime 
     * @return com.cloudwise.trademark.util.Result 
     */
    @Override
    public Map<String, Object> showAgentChart(String startTime, String endTime) {
        if (startTime == null || "".equals(startTime)) {
            startTime = "2000-01-01";
        }
        if (endTime == null || "".equals(endTime)) {
            endTime = "2100-01-01";
        }
        List<Map<String, Object>> list = businessDao.showAgentChart(startTime, endTime);
        Map<String, Object> map = new HashMap<>();
        //X轴
        String[] X = new String[list.size()];
        //y轴
        Long[] Y = new Long[list.size()];
        for (int i = 0; i < list.size(); i++) {
            X[i] = list.get(i).get("login_name").toString();
            Y[i] = (Long) list.get(i).get("count(*)");
        }
        map.put("X", X);
        map.put("Y", Y);
        return map;
    }

}
